package jehc.djshi.sys.service.impl;
import java.util.List;
import java.util.Map;
import cn.hutool.core.collection.CollectionUtil;
import jehc.djshi.common.constant.SysContanst;
import jehc.djshi.common.util.QR.QRUtil;
import jehc.djshi.sys.dao.XtEncoderQrcodeDao;
import jehc.djshi.sys.model.XtEncoderQrcode;
import jehc.djshi.sys.service.XtEncoderQrcodeService;
import org.springframework.stereotype.Service;
import jehc.djshi.common.base.BaseService;
import jehc.djshi.common.util.ExceptionUtil;
import javax.annotation.Resource;
/**
 * @Desc 平台二维码
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Service
public class XtEncoderQrcodeServiceImpl extends BaseService implements XtEncoderQrcodeService {

	@Resource
	private XtEncoderQrcodeDao xtEncoderqrcodeDao;

	@Resource
	private QRUtil qrUtil;

	/**
	* 分页
	* @param condition 
	* @return
	*/
	public List<XtEncoderQrcode> getXtEncoderQrcodeListByCondition(Map<String,Object> condition){
		try{
			List<XtEncoderQrcode> encoderQrcodeList = xtEncoderqrcodeDao.getXtEncoderQrcodeListByCondition(condition);
			if(CollectionUtil.isNotEmpty(encoderQrcodeList)){
				for(XtEncoderQrcode encoderQrcode: encoderQrcodeList){
					String base64 = qrUtil.encodeImageBase64( "png",encoderQrcode.getContent(), 430, 430, SysContanst.LOGO_BASE64);
					encoderQrcode.setQrImage(base64);
				}
			}
			return encoderQrcodeList;
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	* 查询对象
	* @param id
	* @return
	*/
	public XtEncoderQrcode getXtEncoderQrcodeById(String id){
		try{
			XtEncoderQrcode xtEncoderQrcode = xtEncoderqrcodeDao.getXtEncoderQrcodeById(id);
			String base64 = qrUtil.encodeImageBase64( "png",xtEncoderQrcode.getContent(), 430, 430, SysContanst.LOGO_BASE64);
			xtEncoderQrcode.setQrImage(base64);
			return xtEncoderQrcode;
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	* 添加
	* @param xtEncoderQrcode
	* @return
	*/
	public int addXtEncoderQrcode(XtEncoderQrcode xtEncoderQrcode){
		int i = 0;
		try {
//			String base64 = qrUtil.encodeImageBase64( "png",xtEncoderqrcode.getContent(), 430, 430, SysContanst.LOGO_BASE64);
//			String newName = AllUtils.getRandom()+".png";
//			String path = getXtPathCache("xt_encoderqrcode_default_path").get(0).getXt_path();
//			String relative_path = getXtPathCache("xt_encoderqrcode_relative_path").get(0).getXt_path();
//			File f = new File(path);
//			if(!f.exists()){
//				f.mkdirs();
//			}
			xtEncoderQrcode.setCreateId(getXtUid());
			xtEncoderQrcode.setCreateTime(getDate());
////			twoDimensionCode.encoderQRCode(xtEncoderqrcode.getXt_encoderqrcode_url(), path+newName, "png");
//			XtAttachment xt_Attachment = new XtAttachment();
//			xt_Attachment.setXt_attachment_id(UUID.toUUID());
//			xt_Attachment.setXt_attachmentCtime(DateUtil.getSimpleDateFormat());
//			xt_Attachment.setXt_attachmentName(newName);
//			xt_Attachment.setXt_attachmentPath(relative_path+newName);
//			xt_Attachment.setXt_userinfo_id(getXtUid());
//			xt_Attachment.setXt_attachmentType("image/png");
//			xt_Attachment.setXt_attachmentTitle(xtEncoderqrcode.getXt_encoderqrcode_title());
//			xt_Attachment.setXt_userinfo_id(getXtUid());
//			xtAttachmentService.addXtAttachment(xt_Attachment);
//			xtEncoderqrcode.setXt_attachment_id(xt_Attachment.getXt_attachment_id());
			i = xtEncoderqrcodeDao.addXtEncoderQrcode(xtEncoderQrcode);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 修改
	* @param xtEncoderQrcode
	* @return
	*/
	public int updateXtEncoderQrcode(XtEncoderQrcode xtEncoderQrcode){
		int i = 0;
		try {
			xtEncoderQrcode.setUpdateId(getXtUid());
			xtEncoderQrcode.setUpdateTime(getDate());
//			String newName = AllUtils.getRandom()+".png";
//			String path = getXtPathCache("xt_encoderqrcode_default_path").get(0).getXt_path();
//			String relative_path = getXtPathCache("xt_encoderqrcode_relative_path").get(0).getXt_path();
//			File f = new File(path);
//			if(!f.exists()){
//				f.mkdirs();
//			}
//			XtAttachment xt_Attachment = new XtAttachment();
//			xt_Attachment.setXt_attachment_id(UUID.toUUID());
//			xt_Attachment.setXt_attachmentCtime(getSimpleDateFormat());
//			xt_Attachment.setXt_attachmentName(newName);
//			xt_Attachment.setXt_attachmentPath(relative_path+newName);
//			xt_Attachment.setXt_userinfo_id(getXtUid());
//			xt_Attachment.setXt_attachmentType("image/png");
//			xt_Attachment.setXt_attachmentTitle(xtEncoderqrcode.getXt_encoderqrcode_title());
//			xt_Attachment.setXt_userinfo_id(getXtUid());
//			xtAttachmentService.addXtAttachment(xt_Attachment);
//			xtEncoderqrcode.setXt_attachment_id(xt_Attachment.getXt_attachment_id());
			i = xtEncoderqrcodeDao.updateXtEncoderQrcode(xtEncoderQrcode);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 删除
	* @param condition 
	* @return
	*/
	public int delXtEncoderQrcode(Map<String,Object> condition){
		int i = 0;
		try {
			i = xtEncoderqrcodeDao.delXtEncoderQrcode(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}
}
